package hl.alde;

public class Sort {

    static int[] b = {0,0,0,0,0};

    public static void merge(int[] c, int[] d, int l, int m, int r){
        int i = l,
                j = m + l,
                k = l;
        while ((i <= m )&&(j <= r)){
            if(c[i] <= c[j] )
                d[k++] = c[i++];
            else
                d[k++] = c[j++];
        }

        if(i > m)
            for (int q = j; q <= r; q++)
                d[k++] = c[q];
        else
            for (int q = i; q <= m; q++)
                d[k++] = c[q];
    }

    public static void mergeSort(int a[], int left, int right){
        if(left < right){
            int middle = (left + right) / 2;
            mergeSort(a, left, middle);
            mergeSort(a, middle+1, right);

            merge(a, b, left, middle, right);
        }
    }

    public static void main(String[] args) {
        int[] a={0,2,1,3,6};
        mergeSort(a, 0, 4);
        for (int k:b) {
            System.out.println(k);
        }

/*
        mergeSort(a, 0, 3); 四个元素
            if(0 < 3) true
                i = (0+3) / 2 = 1;
                mergesort(a, 0, 1);
                    if(0 < 1>) true
                        i = (0+1) / 2 = 0;
                        mergesort(a, 0, 0);
                            if(0 < 0) false
                            return
                        mergesort(a, 1, 1);
                            if(1 < 1) false
                            return
                        merge(a, b, 0, 1);
                        copy(a, b, 0, 1);

                mergesort(a, 2, 3);
                    f(2 < 3>) true
                        i = (2+3) / 2 = 2;
                        mergesort(a, 2, 2);
                            if(0 < 0) false
                            return
                        mergesort(a, 3, 3);
                            if(1 < 1) false
                            return
                        merge(a, b, 2, 3);
                        copy(a, b, 2, 3);

                merge(a, b, 0, 3);
                copy(a, b, 0, 3);
 */
    }
}
